package com.ulook.planetmanager.ui.home;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ulook.planetmanager.base.BaseViewModel2;
import com.ulook.planetmanager.common.AppServer;
import com.ulook.planetmanager.model.TUser;
import com.ulook.planetmanager.util.json.JacksonUtil;
import com.ulook.planetmanager.util.net.AsynRequestUtil;
import com.ulook.planetmanager.util.net.OkHttpException;
import com.ulook.planetmanager.util.net.RequestParams;
import com.ulook.planetmanager.util.net.ResponseCallback;

import java.util.List;

public class HomeViewModel extends BaseViewModel2 {

    private MutableLiveData<List<TUser>> users = new MutableLiveData<>();

    public LiveData<List<TUser>> getUsers(){
        return users;
    }

    /**
     * 搜索用户
     */
    public void searchUser(String key){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TUser> tUserList = JacksonUtil.json2list((String)res, TUser.class);
                    users.setValue(tUserList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("查询失败");
            }
        };
        RequestParams params = new RequestParams();
        params.put("key", key);
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/manage/searchUser", params, null, callback);
    }
}